package cn.lcl.basic.dao.impl;

import cn.lcl.basic.dao.IBookDao;
import cn.lcl.basic.po.Book;
import org.apache.log4j.Logger;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;



/**
 * Created by licho on 2017/8/9 0009.
 */
@Repository("bookDao")
public class BookDaoImp extends AbstractBaseDao<Book> implements IBookDao {
    private static final Logger log= Logger.getLogger(BookDaoImp.class);

    /**
     * 通过setter方法注入sessionFactory
     * @param factory
     */
    @Autowired
    public void setFactory(SessionFactory factory) {
        super.factory = factory;
    }


    @Override
    public void test() {
        System.out.println("测试Dao层方法");
    }

    @Override
    public void selectById(Long id) {
        Session s=factory.getCurrentSession();
        s.beginTransaction();
        try{
           Book book= s.get(Book.class,id);
            log.debug("book的值:"+book);
            s.getTransaction().commit();
        }catch (Exception e){
            s.getTransaction().rollback();
            e.printStackTrace();
        }
    }
}
